<?php

include_once($_SERVER["DOCUMENT_ROOT"]. "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/mblog/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/config/inc.php");
$g_cgival = array();
$g_show = array();
$g_pro = array();
if(! security_check::check_refer())
{
	throw new Exception("来路不明");
}


function check_cgi_pro()
{
	global $g_cgival, $g_show, $g_pro;
	cgi::both($g_cgival['t'],"t","json");
	$g_cgival['t'] = string::un_html($g_cgival['t']);
	
	cgi::both($g_cgival['cb'],"cb","");
	$g_cgival['cb'] = string::un_html($g_cgival['cb']);
	
	cgi::both($g_cgival['form_id'],"form_id","");
	$g_cgival['form_id'] = string::un_html($g_cgival['form_id']);
	
	
	cgi::both($g_cgival['callback'],"callback","");
	$g_cgival['callback'] = string::un_html($g_cgival['callback']);
	
	cgi::both($g_cgival['pic_name'],"pic_name","");
	$g_cgival['pic_name'] = string::un_html($g_cgival['pic_name']);
	
	cgi::both($g_cgival['thumbstatus'],"thumbstatus",1);
	if($g_cgival['thumbstatus'] != 1)
	{
		$g_cgival['thumbstatus'] = 2;
	}
	
	cgi::both($g_cgival['thumbsize'],"thumbsize","100|75");
	$g_cgival['thumbsize'] = string::un_html($g_cgival['thumbsize']);
	
		
	
}


function get_data()
{
	global $g_cgival, $g_show, $g_pro;
	
	$pic_content = base64_encode(file_get_contents($_FILES[$g_cgival['pic_name']]["tmp_name"]));
	$mblog_from_api = new mblog_from_api();
	$res = $mblog_from_api->api_upload_pic($pic_content);
	
	
	
	$g_show["pic1"] = $res["data"]["result"]["original_pic"];
	$g_show["pic2"] = $res["data"]["result"]["thumbnail_pic"];
	$g_show["pic3"] = $res["data"]["result"]["bmiddle_pic"];
	$g_show["pic_id"] = $res["data"]["result"]["pic_id"];
	
	$g_show["size"] = $_FILES[$g_cgival['pic_name']]["size"];
	
	$pic_name = $_FILES[$g_cgival['pic_name']]["name"];
	$encode = mb_detect_encoding($pic_name, array('ASCII','GB2312','GBK','UTF-8'));
	
	
	mb_convert_encoding($pic_name,"UTF-8","GBK");
	$g_show["pic_name"] = urlencode($pic_name);
	
	
	
	$res = getimagesize($_FILES[$g_cgival['pic_name']]["tmp_name"]);
	
	$srcW = $res["0"]; 
	$srcH = $res["1"]; 
	
	$g_show["p_width"] = PUBU_WIDTH;
	$g_show["p_height"] = intval($srcH/($srcW/$g_show["p_width"] ));
	
	unlink($_FILES[$g_cgival['pic_name']]["tmp_name"]);
}

 
try
{
	check_cgi_pro();
	get_data();

	$g_show["error"] = "0";
	$g_show["errmsg"] = "";
}
catch (Exception $e)
{
	$g_show["error"] = "1";
	$g_show["errmsg"] = $e->getMessage();
}
show_pro();
exit();


function show_pro()
{
	global $g_cgival, $g_show, $g_pro;
	

	if($g_cgival['cb'])
	{
		header("Location:".$g_cgival['cb']."?pic1={$g_show["pic1"]}&pic2={$g_show["pic2"]}&pic3={$g_show["pic3"]}&pic_name={$g_show["pic_name"]}&size={$g_show["size"]}&pid={$g_show["pic_id"]}&p_width={$g_show["p_width"]}&p_height={$g_show["p_height"]}&error={$g_show["error"]}&errormsg=".urlencode($g_show["errmsg"]));
	}
	else 
	{
		echo output::json($g_cgival["t"], $g_show, $g_cgival["callback"]);
	}
}